import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";

export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      "@": resolve(__dirname, "src"),
      components: resolve(__dirname, "src/components"),
      views: resolve(__dirname, "src/views"),
      stores: resolve(__dirname, "src/stores"),
      services: resolve(__dirname, "src/services"),
      assets: resolve(__dirname, "src/assets"),
      utils: resolve(__dirname, "src/utils"),
    },
  },
  server: {
    proxy: {
      // 统一所有API请求到同一个后端端口
      "/api": {
        target: process.env.VITE_API_BASE_URL || "http://127.0.0.1:8001",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, "/api"),
      },
      "/ws": {
        target: process.env.VITE_API_BASE_URL || "http://127.0.0.1:8001",
        changeOrigin: true,
        ws: true,
      },
      "/v1": {
        target: process.env.VITE_API_BASE_URL || "http://127.0.0.1:8001",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/v1/, "/v1"),
      },
    },
  },
  build: {
    outDir: "dist",
    assetsDir: "assets",
    sourcemap: false,
    minify: "terser",
    rollupOptions: {
      output: {
        chunkFileNames: "js/[name]-[hash].js",
        entryFileNames: "js/[name]-[hash].js",
        assetFileNames: "[ext]/[name]-[hash].[ext]",
      },
    },
  },
});
